<?php

namespace app\admin\model;

use app\common\model\UserRule;
use think\Db;
use think\Model;


class Account extends Model
{

    //添加活动参与人类型  1 前端购买 2 后台添加
    const ADD_ACTIVITY_TYPE_1 = 1;
    const ADD_ACTIVITY_TYPE_2 = 2;
    const USER_SOURCE_99 = 99;//用户来源 99代表是 zoom用户


    // 表名
    protected $table = 'account';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = false;

    // 追加属性
    protected $append = [
        'prevtime_text',
        'logintime_text',
        'operationtime_text',
        'source_text',
        'TLS_text',
        'isOld_text'
    ];


    
    public function getSourceList()
    {
        return ['0' => __('Source 0'), '1' => __('Source 1'), '2' => __('Source 2'), '3' => __('Source 3'), '4' => __('Source 4'), '5' => __('Source 5')];
    }

    public function getTlsList()
    {
        return ['1' => __('Tls 1'), '0' => __('Tls 0')];
    }

    public function getIsoldList()
    {
        return ['0' => __('Isold 0'), '1' => __('Isold 1')];
    }


    public function getPrevtimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['prevtime']) ? $data['prevtime'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getLogintimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['logintime']) ? $data['logintime'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getOperationtimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['operationtime']) ? $data['operationtime'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getSourceTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['source']) ? $data['source'] : '');
        $list = $this->getSourceList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getTlsTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['TLS']) ? $data['TLS'] : '');
        $list = $this->getTlsList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getIsoldTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['isOld']) ? $data['isOld'] : '');
        $list = $this->getIsoldList();
        return isset($list[$value]) ? $list[$value] : '';
    }

    protected function setPrevtimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    protected function setLogintimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    protected function setOperationtimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    /*
     * 通过手机号获取用户信息
     */
    public static function getByMobile($mobile){
        if(empty($mobile))
            return false;
        $account = self::where('mobile', $mobile)->field('id,username,nickname,mobile')->find();
        if($account)
            return $account;
        else
            return '';
    }

}
